package team.you.change.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import team.you.change.util.Result;
import team.you.change.pojo.User;
import team.you.change.service.LoginService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//被Springmvc扫描 表明这个类表示控制层
@Controller
//请求地址路径映射
@RequestMapping("/login")
public class LoginController {
    //spring可以自动帮你把bean里面引用的对象的setter/getter方法省略，它会自动帮你set/get;对类成员变量、方法、构造函数进行标注，完成自动装配功能。
    @Autowired
    private LoginService loginService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");//判断是否免密
        if (user != null) {
            return "index";//用户不为空，则进入主页
        } else return "login";//用户为空进入登录页
    }

    //跳转注册页面
    @RequestMapping("/goRegister")
    public String register() {
        return "register";
    }

    @ResponseBody
    @RequestMapping("/registerOn")
    private Result registerOn(User user) {
        return loginService.register(user);
    }

    @RequestMapping("/selUsername")
    @ResponseBody
    private Result selUsername(String username) {
        return loginService.selUsername(username);
    }

    @ResponseBody
    @RequestMapping("/selTeaNo")
    private Result selTeaNo(String papers) {
        return loginService.selTeaNo(papers);
    }

    @RequestMapping("/codeVarify")
    @ResponseBody
    public Result codeVarify(String testCode, Integer type, HttpServletRequest request) {
        return loginService.codeVarify(testCode, type, request);
//        return new Result();
    }
    @RequestMapping("/mobileCode")
    @ResponseBody
    public Result mobileCode(String phone,HttpServletRequest request){
        return loginService.mobileCode(phone,request);
    }

    //跳转登录页面
    @RequestMapping("/goLogin")
    private String returnLogin() {
        return "login";
    }

    @RequestMapping("/getCode")
    private void getCode(HttpServletRequest request, HttpServletResponse response) {
        loginService.getCode(request, response);
    }

    @RequestMapping("/login")
    @ResponseBody
    public Result login(String remember, String username, String password, String code, HttpServletRequest request) {
        return loginService.login(remember, username, password, code, request);
    }

    //退出登录
    @RequestMapping("/loginOut")
    private String loginOut(HttpServletRequest request) {
        request.getSession().invalidate();//清除session,忘记密码
        return "login";
    }
}
